import util from "../../../../utils/util";
const app = getApp();
var cityInfo = [];
var shebaoMax = 0;
var gongjijinMax = 0;
var shebaoMin = 0;
var gongjijinMin = 0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        itemIndex: 0,
        city: [],
        cityIndex: 0,
        type: ["职工", "居民"],
        typeIndex: 0,
        title: ["项目", "基数", "个人", "企业", "小计"],
        calculators: [{
            name: "养老",
            base: 0,
            perNum: 0,
            perProp: 0,
            comNum: 0,
            comProp: 0,
            total: 0,
            isDone: false,
            computing: true,
        }, {
            name: "医疗",
            base: 0,
            perNum: 0,
            perProp: 0,
            comNum: 0,
            comProp: 0,
            total: 0,
            isDone: false,
            computing: false,
        }, {
            name: "工伤",
            base: 0,
            perNum: 0,
            perProp: 0,
            comNum: 0,
            comProp: 0,
            total: 0,
            isDone: false,
            computing: false,
        }, {
            name: "生育",
            base: 0,
            perNum: 0,
            perProp: 0,
            comNum: 0,
            comProp: 0,
            total: 0,
            isDone: false,
            computing: false,
        }, {
            name: "失业",
            base: 0,
            perNum: 0,
            perProp: 0,
            comNum: 0,
            comProp: 0,
            total: 0,
            isDone: false,
            computing: false,
        }, {
            name: "公积金",
            base: 0,
            perNum: 0,
            perProp: 0,
            comNum: 0,
            comProp: 0,
            total: 0,
            isDone: false,
            computing: false,
        }, ],
        calIndex: 0,
        calName: "养老",
        base: "",
        per: "",
        com: "",
        perTotal: 0,
        comTotal: 0,
        total: 0,
        shebaoMin: 0,
        shebaoMax: 0,
        gongjijinMin: 0,
        gongjijinMax: 0,
        name: "",
        path: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        util.getUrl(app.globalData.url).then(res => {
            this.getCity();
        });
    },

    tabToggle(e) {
        let itemIndex = e.currentTarget.dataset.index;
        this.setData({
            itemIndex,
        });
    },

    removeAll() {
        this.data.calculators.forEach((item, index) => {
            item.base = 0;
            item.perNum = 0;
            item.perProp = 0;
            item.comNum = 0;
            item.comProp = 0;
            item.total = 0;
            item.isDone = false;
            item.computing = index == this.data.calIndex ? true : false;
        });
        this.setData({
            calculators: this.data.calculators,
            base: 0,
            per: 0,
            com: 0,
            perTotal: 0,
            comTotal: 0,
            total: 0,

        })
    },

    getCity() {
        util.request("zhuanti.php", {
            action: "ss_type"
        }, ).then(res => {
            if (res.data.message == "success") {
                let city = [];
                res.data.data.forEach(item => city.push(item.cityname));
                cityInfo = res.data.data;
                this.setData({
                    city,
                    type: res.data.data[0].type,
                });
                this.getCityInfo();
            }
        })

    },
    citySelect(e) {
        let cityIndex = e.detail.value;
        let type = cityInfo[cityIndex].type;
        this.setData({
            type,
            cityIndex,
            typeIndex: 0,
        });
        this.getCityInfo();
    },

    typeSelect(e) {
        let typeIndex = e.detail.value;
        this.setData({
            typeIndex,
        });
        this.getCityInfo();
    },

    getCityInfo() {
        util.request("zhuanti.php", {
            action: "wuxianyijin",
            citycode: cityInfo[this.data.cityIndex].citycode,
            type: this.data.type[this.data.typeIndex],
        }, ).then(res => {
            if (res.data.message == "success") {
                shebaoMax = Number(res.data.data.ss.shebao.max);
                shebaoMin = Number(res.data.data.ss.shebao.min);
                gongjijinMax = Number(res.data.data.ss.gjj.max);
                gongjijinMin = Number(res.data.data.ss.gjj.min);
                this.setData({
                    shebaoMin,
                    shebaoMax,
                    gongjijinMin,
                    gongjijinMax,
                    name: res.data.data.ss.title,
                    path: res.data.data.ss.url,
                });
            }
        })
    },

    choseItem(e) {
        let calIndex = e.currentTarget.dataset.index;
        let info = this.data.calculators[calIndex];
        this.setData({
            calIndex,
        });
        this.data.calculators.forEach((item, index) => {
            if (index == calIndex && !item.isDone)
                item.computing = true;
            else
                item.computing = false;
        });
        this.setData({
            calculators: this.data.calculators,
            calName: this.data.calculators[calIndex].name,
            per: info.perProp ? info.perProp : "",
            com: info.comProp ? info.comProp : "",
            base: info.base ? info.base : "",
            itemIndex: 0,
        });
    },

    baseNumInput(e) {
        let input = parseFloat(e.detail.value);
        let type = e.currentTarget.dataset.type;
        let info = this.data.calculators[this.data.calIndex];
        if (type == "per") {
            if (this.data.calIndex == 5) {
                if (input < 5) {
                    this.setData({
                        per: 5
                    });
                    info.perProp = this.data.per ? this.data.per : 0;
                } else if (input > 12) {
                    this.setData({
                        per: 12,
                    });
                    info.perProp = this.data.per ? this.data.per : 0;
                } else
                    info.perProp = input;
            } else
                info.perProp = input ? input : 0;

        } else if (type == "com") {
            if (this.data.calIndex == 5) {
                if (input < 5) {
                    this.setData({
                        com: 5
                    });
                    info.comProp = this.data.com ? this.data.com : 0;
                } else if (input > 12) {
                    this.setData({
                        com: 12,
                    });
                    info.comProp = this.data.com ? this.data.com : 0;
                } else
                    info.comProp = input ? input : 0;
            } else
                info.comProp = input;
        } else {
            if (this.data.calIndex == 5) {
                if (input < gongjijinMin) {
                    this.setData({
                        base: gongjijinMin
                    });
                    info.base = this.data.base ? this.data.base : 0;
                } else if (input > gongjijinMax) {
                    this.setData({
                        base: gongjijinMax,
                    });
                    info.base = this.data.base ? this.data.base : 0;
                } else
                    info.base = input ? input : 0;
            } else {
                if (input < shebaoMin) {
                    this.setData({
                        base: shebaoMin
                    });
                    info.base = this.data.base ? this.data.base : 0;
                } else if (input > shebaoMax) {
                    this.setData({
                        base: shebaoMax,
                    });
                    info.base = this.data.base ? this.data.base : 0;
                } else
                    info.base = input ? input : 0;
            }

        }
        this.setData({
            calculators: this.data.calculators,
        });
    },

    computed(e) {
        let parms = e.detail.value;
        let info;
        if (parms.base && parms.per && parms.com) {
            this.comResult();
            this.setData({
                itemIndex: 1,
            });
        }

    },

    clear() {
        this.data.calculators[this.data.calIndex].base = 0;
        this.data.calculators[this.data.calIndex].perNum = 0;
        this.data.calculators[this.data.calIndex].perProp = 0;
        this.data.calculators[this.data.calIndex].comNum = 0;
        this.data.calculators[this.data.calIndex].comProp = 0;
        this.data.calculators[this.data.calIndex].total = 0;
        this.data.calculators[this.data.calIndex].isDone = false;
        this.data.calculators[this.data.calIndex].computing = false;
        this.setData({
            calculators: this.data.calculators,
        });
        this.comResult("clear");
    },

    comResult(clear) {
        let info = this.data.calculators[this.data.calIndex];
        info.perNum = (info.base * info.perProp * 0.01).toFixed(2);
        info.comNum = (info.base * info.comProp * 0.01).toFixed(2);
        info.isDone = clear ? false : true;
        info.computing = clear ? true : false;
        info.total = parseFloat(info.perNum) + parseFloat(info.comNum)
        this.setData({
            calculators: this.data.calculators,
        });
        this.data.perTotal = this.data.calculators.reduce((totla, item) => totla += parseFloat(item.perNum), 0);
        this.data.comTotal = this.data.calculators.reduce((totla, item) => totla += parseFloat(item.comNum), 0);
        this.data.total = this.data.perTotal + this.data.comTotal
        this.setData({
            perTotal: this.data.perTotal.toFixed(2),
            comTotal: this.data.comTotal.toFixed(2),
            total: this.data.total.toFixed(2),
        });
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})